package org.ceteca.explica.client.gui;

import org.apache.log4j.Logger;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.InterfaceExplica;
import org.ceteca.javaglade.Gui;
import org.ceteca.javaglade.InterfaceGladeGtk;
import org.ceteca.javaglade.JavaGladeException;
import org.ceteca.javaglade.handlers.GenericHandler;
import org.ceteca.javaglade.toolkit.swt.SwtDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

public class ExplicaValueInput extends GenericHandler {

	private String valueWidgetInputGUIName;
	private SwtDialog dialogValueInput;
	private Logger logger;
	private String value;
	private Text valueWidget;
//	private CommonClient entityLauncher;
	private Button okButton, cancelButton;
	private Gui parentGui;
	private String selectedMethod;
	private GenericHandler selectedListener;
	
	
	//public ExplicaValueInput() {
	//	super();
		
	//}
	
	public ExplicaValueInput(Logger log, Gui parentGui, String selectedMethod, GenericHandler selectedListener){
		super();
		this.logger=log;
		this.value = null;
		this.valueWidgetInputGUIName = parentGui.getGuiName() + "\\"
		+ "Explica_ValueInput";
		this.parentGui = parentGui;
		this.selectedMethod = selectedMethod;
		this.selectedListener = selectedListener;
	}
	
	public void build(){
		try {
			this.gui = ExplicaGUIBuilder.getInstance().buildGUI(
					valueWidgetInputGUIName,
					InterfaceExplica.EXPLICA_VALUE_INPUT_GLADE_PATH,
					parentGui,
					parentGui.getParentWindow());

			getWidgets();
			Listener[] list=cancelButton.getListeners(SWT.Selection);
			for (Listener l:list){
				cancelButton.removeListener(SWT.Selection, l);
				
			}
			Listener[] list2=okButton.getListeners(SWT.Selection);
			for (Listener l:list2){
				okButton.removeListener(SWT.Selection, l);
				
			}
			ExplicaGUIBuilder.getInstance().connectSignal(InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED, cancelButton, "on_cancel_button_clicked", this);
			ExplicaGUIBuilder.getInstance().connectSignal(InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED, okButton, "on_ok_button_clicked", this);
			ExplicaGUIBuilder.getInstance().connectSignal(InterfaceGladeGtk.GTK_SIGNAL_TYPE_CLICKED, okButton, this.selectedMethod, this.selectedListener);
			
			this.dialogValueInput.getShell().setDefaultButton(okButton);

		} catch (JavaGladeException e) {
			logger.error("Error building Map Dialog");
			e.printStackTrace();

		}
		
	}
	
	
	private void getWidgets(){
		if (this.gui != null){
			valueWidget=(Text)this.gui.getWidget(InterfaceExplica.VALUE_INPUT_VALUE_WIDGETNAME);
			dialogValueInput = this.gui.getDialog(
					InterfaceExplica.EXPLICA_VALUE_INPUT_DIALOG_NAME);
			okButton=(Button)this.gui.getWidget(InterfaceExplica.VALUE_INPUT_OK_BUTTON_WIDGETNAME);
			cancelButton=(Button)this.gui.getWidget(InterfaceExplica.VALUE_INPUT_CANCEL_BUTTON_WIDGETNAME);
		}
		if (valueWidget== null || dialogValueInput==null || okButton==null || cancelButton==null)
			System.exit(-1);
	}
	
	
	public void show(String value){
		valueWidget.setText(value);
		dialogValueInput.open();
	}
	
	
	public void on_cancel_button_clicked(Event e){
		this.value = null;
		dialogValueInput.close();
	}
	
	public void on_ok_button_clicked(Event e){
		this.value = valueWidget.getText();
		System.out.println("Introducido nuevo valor..." + getValue());
		dialogValueInput.close();

	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}
